package com.dyh.quickdemo.demos.lbdFactoryBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Function;

/*
@Author=Joey Wheeler
@Date=2021/1/11 14:18
*/
public class FunctionEnhancer {

    Logger logger = LoggerFactory.getLogger(this.getClass());

//    private FunctionWrap functionWrap;

    private Function target;
    private String id;

    private FunctionEnhancer(//String id,
                             Function target){
      //  this.id = id;
        this.target = target;
    }

    private FunctionEnhancer(String id,
                             Function target){
        this.id = id;
        this.target = target;
    }

    public  static FunctionEnhancer with(//String id,
                                        Function target){
        FunctionEnhancer enhancer = new FunctionEnhancer(target);
        return enhancer;
    }

    public  static FunctionEnhancer with(String id,
                                         Function target){
        FunctionEnhancer enhancer = new FunctionEnhancer(id,target);
        return enhancer;
    }

    public Function build(){
        return this.target;
    }

    public Function regist(){
        FunctionNodeCenter.regist(this.id,this.target);
        return this.target;
    }


    /*
    * 把异常隐藏掉
    * */
    public  FunctionEnhancer hideException(){
        Function that = this.target;
        this.target = e->{
            Object result = null;
            try {
                result = that.apply(e);
            } catch (Exception ex) {

            }
            return result;
        };
        return this;
    }
    //	at com.dyh.quickdemo.demos.lbdFactoryBean.FunctionEnhancer.lambda$hideException$0(FunctionEnhancer.java:32)

    /*
    *  this.target = e->{
            Object result = null;
            try {
                result = this.target.apply(e);
            } catch (Exception ex) {

            }
            return result;
        };
        return this;
    *
    * */


    public FunctionEnhancer logStartEnd(){
        Function that = this.target;
        this.target = e->{
            Object result = null;
            logger.info("{}-开始执行",this.id);
            result = that.apply(e);
            logger.info("{}-结束执行",this.id);
            return result;
        };
        return this;

    }

    public static void main(String[] args) throws InterruptedException {
        StringBuffer sb = new StringBuffer("111");
        sb.append("222",0,3);
        System.out.println(sb.toString());
//        long t1 = System.nanoTime();
//        Thread.sleep(1000);
//        long t2 = System.nanoTime();
//        System.out.println( (t2-t1)/1000000 );
    }


}
